// GLView.cpp
/*
Copyright (c) 2009, Richard S. Wright Jr.
GLTools Open Source Library
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.

Neither the name of Richard S. Wright Jr. nor the names of other contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "GLView.h"
#include "CubeData.h"

void GetNormalMatrix(M3DMatrix33f mNormal, const M3DMatrix44f mModelView){
	M3DVector4f vColumn;

	m3dGetMatrixColumn44(vColumn, mModelView, 0);
	m3dNormalizeVector3(vColumn);
	m3dSetMatrixColumn33(mNormal, vColumn, 0);

	m3dGetMatrixColumn44(vColumn, mModelView, 1);
	m3dNormalizeVector3(vColumn);
	m3dSetMatrixColumn33(mNormal, vColumn, 1);

	m3dGetMatrixColumn44(vColumn, mModelView, 2);
	m3dNormalizeVector3(vColumn);
	m3dSetMatrixColumn33(mNormal, vColumn, 2);

}
bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode){
	GLbyte* bits;
	int width, height, components;
	GLenum format;

	bits = gltReadTGABits(szFileName, &width, &height, &components, &format);
	if (bits == NULL)
	{
		MessageBox(NULL, "Can't load Texture", szFileName, MB_OK);
		return false;
	}

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);

	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexImage2D(GL_TEXTURE_2D, 0, components, width, height, 0, format, GL_UNSIGNED_BYTE, bits);


	delete[] bits;

	if (minFilter == GL_LINEAR_MIPMAP_LINEAR ||
		minFilter == GL_LINEAR_MIPMAP_NEAREST ||
		minFilter == GL_NEAREST_MIPMAP_LINEAR ||
		minFilter == GL_NEAREST_MIPMAP_NEAREST)
		glGenerateMipmap(GL_TEXTURE_2D);

	return true;
}
bool LoadBMPAsTexture(const char* szFileName, GLenum filter, GLenum wrapMode){

	GLbyte* pBits;
	int nWidth, nHeight;

	pBits = gltReadBMPBits(szFileName, &nWidth, &nHeight);
	if (pBits == NULL)
	{
		MessageBox(NULL, "Can't load Texture", szFileName, MB_OK);
		return false;
	}


	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, nWidth, nHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, pBits);

	if (filter != GL_LINEAR && filter != GL_NEAREST)
		glGenerateMipmap(GL_TEXTURE_2D);

	free(pBits);

	return true;
}

////////////////////////////////////////////////////////////////////////////
// Do not put any OpenGL code here. General guidence on constructors in 
// general is to not put anything that can fail here either (opening files,
// allocating memory, etc.)
GLView::GLView(void) : screenWidth(800), screenHeight(600), bFullScreen(false), bAnimated(true), samples(1), bGameMode(false)
{
	buttonState = 0;
	joyState = 0;
}


///////////////////////////////////////////////////////////////////////////////
// OpenGL related startup code is safe to put here. Load textures, etc.
void GLView::Initialize(void)
{
	// Make sure OpenGL entry points are set
	glewInit();

	moveSpeed = 10.0f;
	turnSpeed = 60.0f;

	M3DVector4f pos = { 0.0f, -3.0f, 0.0f, 1.0f };
	memcpy(lightPos, pos, sizeof(M3DVector4f));

	glClearColor(0.8f, 0.4f, 0.4f, 1.0f);

	hShader = gltLoadShaderPairWithAttributes("Resources/Shaders/base.vp", "Resources/Shaders/base.fp", 3, GLT_ATTRIBUTE_VERTEX, "vVertexPos", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexCoord");
	hUniMVP = glGetUniformLocation(hShader, "mMVP");
	hUniModelView = glGetUniformLocation(hShader, "mModelView");
	hUniNormalMatrix = glGetUniformLocation(hShader, "mNormalMatrix");
	hUniLightPos = glGetUniformLocation(hShader, "vLightPos");
	hUniNormalToggle = glGetUniformLocation(hShader, "bNormalToggle");
	sampler = glGetUniformLocation(hShader, "textureMap");

	hCustomCubeShader = gltLoadShaderPairWithAttributes("Resources/Shaders/base.vp", "Resources/Shaders/base.fp", 4, ATTRIBUTE_VERTEX, "vVertexPos", ATTRIBUTE_NORMAL, "vNormal", ATTRIBUTE_TEXTURE0, "vTexCoord", ATTRIBUTE_TANGENT, "vVertexTangent");
	hUniMVPCCS = glGetUniformLocation(hShader, "mMVP");
	hUniModelViewCCS = glGetUniformLocation(hShader, "mModelView");
	hUniNormalMatrixCCS = glGetUniformLocation(hShader, "mNormalMatrix");
	hUniLightPosCCS = glGetUniformLocation(hShader, "vLightPos");
	hUniNormalToggleCCS = glGetUniformLocation(hShader, "bNormalToggle");
	samplerCCS = glGetUniformLocation(hShader, "textureMap");


	glUseProgram(hShader);
	glUniform1i(sampler, 0);
	sampler = glGetUniformLocation(hShader, "normalMap");
	glUniform1i(sampler, 1);

	glUseProgram(hCustomCubeShader);
	glUniform1i(samplerCCS, 0);
	samplerCCS = glGetUniformLocation(hCustomCubeShader, "normalMap");
	glUniform1i(samplerCCS, 1);

	if (hCustomCubeShader == NULL || hUniMVPCCS < 0 || hUniModelViewCCS < 0 || hUniNormalMatrixCCS < 0
		|| hUniLightPosCCS < 0 || samplerCCS < 0 || hUniNormalToggleCCS < 0)
		MessageBox(NULL, "PROBLEM WITH CC SHADER", NULL, MB_OK);


	gltMakeSphere(sphere, 1, 50, 25);
	gltMakeCube(cube, 0.5f);

	//Load Textures
	glGenTextures(1, &brickTexture);
	glBindTexture(GL_TEXTURE_2D, brickTexture);
	LoadTGATexture("Resources/Images/BrickTexture.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE);

	glGenTextures(1, &brickNormal);
	glBindTexture(GL_TEXTURE_2D, brickNormal);
	LoadTGATexture("Resources/Images/BrickNormal.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE);

	glGenTextures(1, &planetTexture);
	glBindTexture(GL_TEXTURE_2D, planetTexture);
	LoadTGATexture("Resources/Images/IceMoon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE);

	glGenTextures(1, &planetNormal);
	glBindTexture(GL_TEXTURE_2D, planetNormal);
	LoadTGATexture("Resources/Images/IceMoonBump.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE);

	bNormalToggle = true;
	glEnable(GL_DEPTH_TEST);

	CreateCube(0.5f);

}

void GLView::CreateCube(GLfloat radius){

	//Set Up VAO
	glGenVertexArrays(1, &cubeVAO);
	glBindVertexArray(cubeVAO);

	GLfloat vCubeVerts[36 * 3] = {
		//Front
		-radius, radius, radius,
		-radius, -radius, radius,
		radius, -radius, radius,
		-radius, radius, radius,
		radius, -radius, radius,
		radius, radius, radius,

		//Back
		radius, radius, -radius,
		radius, -radius, -radius,
		-radius, radius, -radius,
		-radius, radius, -radius,
		radius, -radius, -radius,
		-radius, -radius, -radius,

		//Left
		-radius, radius,-radius,  
		-radius,-radius,-radius,  
		-radius,-radius, radius,  
		-radius, radius,-radius,  
		-radius,-radius, radius,  
		-radius, radius, radius,  

		//Right
		radius, -radius, radius,
		radius, -radius, -radius,
		radius, radius, -radius,
		radius, -radius, radius,
		radius, radius, -radius,
		radius, radius, radius,

		//Top
		-radius, radius, -radius,
		-radius, radius, radius,
		radius, radius, radius,
		-radius, radius, -radius,
		radius, radius, radius,
		radius, radius, -radius,

		//Bottom
		-radius, -radius, radius,
		-radius, -radius, -radius,
		radius, -radius, -radius,
		-radius, -radius, radius,
		radius, -radius, -radius,
		radius, -radius, radius,
	};

	GLuint vCubeIndexes[36] = {
		0, 1, 2, 3, 4, 5,
		6, 7, 8, 9, 10, 11,
		12, 13, 14, 15, 16, 17,
		18, 19, 20, 21, 22, 23,
		24, 25, 26, 27, 28, 29,
		30, 31, 32, 33, 34, 35
	};

	GLfloat vCubeNormals[36 * 3] = {
		//Front (0.0f,0.0f,1.0f)
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,

		//Back (0.0f,0.0f,-1.0f)
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,

		//Left (-1.0f,0.0f,0.0f)
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,

		//Right (1.0f,0.0f,0.0f)
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,

		//Top (0.0f,1.0f,0.0f)
		0.0f, 1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,

		//Bottom (0.0f,-1.0f,0.0f)
		0.0f, -1.0f, 0.0f,
		0.0f, -1.0f, 0.0f,
		0.0f, -1.0f, 0.0f,
		0.0f, -1.0f, 0.0f,
		0.0f, -1.0f, 0.0f,
		0.0f, -1.0f, 0.0f
	};
	GLfloat vCubeTextureCoordinates[36 * 2] = {
		//Front
		0.0f, 0.0f,
		0.0f, 1.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 1.0f,
		1.0f, 0.0f,
		//Back
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		0.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,
		//Left
		0.0f,0.0f,
		0.0f,1.0f,
		1.0f,1.0f,
		0.0f,0.0f,
		1.0f,1.0f,
		1.0f,0.0f,
		//Right
		0.0f, 0.0f,
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,
		//Top
		0.0f, 0.0f,
		0.0f, 1.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 1.0f,
		1.0f, 0.0f,
		//Bottom
		0.0f, 0.0f,
		0.0f, 1.0f,
		1.0f, 1.0f,
		0.0f, 0.0f,
		1.0f, 1.0f,
		1.0f, 0.0f,
	};
	GLfloat vCubeTangents[36 * 3] = {
		//Front
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		//Back
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		-1.0f, 0.0f, 0.0f,
		//Left
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,
		//Right
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		0.0f, 0.0f, -1.0f,
		//Top
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		//Bottom
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,
	};

	//Position
	SetBuffer(36 * 3 * sizeof(float), vCubeVerts, ATTRIBUTE_VERTEX, 3);

	//Normals
	SetBuffer(36 * 3 * sizeof(float), vCubeNormals, ATTRIBUTE_NORMAL, 3);

	//Texture
	SetBuffer(36 * 2 * sizeof(float), vCubeTextureCoordinates, ATTRIBUTE_TEXTURE0, 2);

	//Tangent
	SetBuffer(36 * 3 * sizeof(float), vCubeTangents, ATTRIBUTE_TANGENT, 3);

	GLuint hGenericObject;
	glGenBuffers(1, &hGenericObject);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, hGenericObject);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(unsigned int), vCubeIndexes, GL_STATIC_DRAW);

	glBindVertexArray(0);
}

void GLView::SetBuffer(size_t bufferSize, void* data, int location, int components){
	GLuint hGO; // generic object

	glGenBuffers(1, &hGO);
	glBindBuffer(GL_ARRAY_BUFFER, hGO);
	glBufferData(GL_ARRAY_BUFFER, bufferSize, data, GL_STATIC_DRAW);
	glVertexAttribPointer(location, components, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(location);
}


///////////////////////////////////////////////////////////////////////////////
// Do your cleanup here. Free textures, display lists, buffer objects, etc.
void GLView::Shutdown(void)
{


}


///////////////////////////////////////////////////////////////////////////////
// This is called at least once and before any rendering occurs. If the screen
// is a resizeable window, then this will also get called whenever the window
// is resized.
void GLView::Resize(GLsizei nWidth, GLsizei nHeight)
{
	glViewport(0, 0, nWidth, nHeight);

	// This is a good place to recompute your projection matrix.
	m3dMakePerspectiveMatrix(projMatrix, m3dDegToRad(60.0f), float(nWidth) / float(nHeight), 1.0f, 100.0f);


}


///////////////////////////////////////////////////////////////////////////////
// Render a frame. The owning framework is responsible for buffer swaps,
// flushes, etc.
void GLView::Render(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	MoveCamera();


	modelViewMatrix.PushMatrix();
	{
		M3DMatrix44f mCamera;
		camera.GetCameraMatrix(mCamera);
		modelViewMatrix.MultMatrix(mCamera);

		lightPos[0] = cos(m3dDegToRad(rotationWatch.GetElapsedSeconds() * 20)) * 15;
		lightPos[1] = 0.0f;
		lightPos[2] = sin(m3dDegToRad(rotationWatch.GetElapsedSeconds() * 20)) * 15;


		//Draw light
		DrawSphere(lightPos[0], lightPos[1], lightPos[2]);

		glUniform1i(hUniNormalToggle, bNormalToggle);

		glEnable(GL_CULL_FACE);
		glUseProgram(hCustomCubeShader);
		DrawCube(0.0f, -1.0f, -5.0f);
		DrawCube(-1.5f, -1.0f, -5.0f);
		glUseProgram(hShader);
		DrawSphere(2.0f, -1.0f, -5.0f);

	}
	modelViewMatrix.PopMatrix();


}

void GLView::DrawCube(GLfloat posx, GLfloat posy, GLfloat posz){
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, brickNormal);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, brickTexture);

	M3DMatrix44f mCamera;
	camera.GetCameraMatrix(mCamera);

	modelViewMatrix.PushMatrix();
	{
		modelViewMatrix.Translate(posx, posy, posz);

		//Get Model View Projection Matrix
		M3DMatrix44f mMVP;
		m3dMatrixMultiply44(mMVP, projMatrix, modelViewMatrix.GetMatrix());
		glUniformMatrix4fv(hUniMVP, 1, GL_FALSE, mMVP);

		//Get Model View Matrix
		glUniformMatrix4fv(hUniModelView, 1, GL_FALSE, modelViewMatrix.GetMatrix());

		//Set Normal Matrix
		M3DMatrix33f NormalMatrix;
		//GetNormalMatrix(NormalMatrix,mCamera);
		GetNormalMatrix(NormalMatrix, modelViewMatrix.GetMatrix());
		glUniformMatrix3fv(hUniNormalMatrix, 1, GL_FALSE, NormalMatrix);

		//Set Light Position
		M3DVector4f lightPosEye;
		m3dTransformVector4(lightPosEye, lightPos, mCamera);
		glUniform4fv(hUniLightPos, 1, lightPosEye);

#if 0
		cube.Draw();
#else
		glBindVertexArray(cubeVAO);
		glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
		glBindVertexArray(NULL);

#endif


	}
	modelViewMatrix.PopMatrix();
}

void GLView::DrawSphere(GLfloat posx, GLfloat posy, GLfloat posz){

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, planetNormal);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, planetTexture);

	M3DMatrix44f mCamera;
	camera.GetCameraMatrix(mCamera);

	modelViewMatrix.PushMatrix();
	{
		modelViewMatrix.Translate(posx, posy, posz);
		//modelViewMatrix.Rotate(90, 1.0f, 0.0f, 0.0f);

		//Get Model View Projection Matrix
		M3DMatrix44f mMVP;
		m3dMatrixMultiply44(mMVP, projMatrix, modelViewMatrix.GetMatrix());
		glUniformMatrix4fv(hUniMVP, 1, GL_FALSE, mMVP);

		//Get Model View Matrix
		glUniformMatrix4fv(hUniModelView, 1, GL_FALSE, modelViewMatrix.GetMatrix());

		//Set Normal Matrix
		M3DMatrix33f NormalMatrix;
		//GetNormalMatrix(NormalMatrix,mCamera);
		GetNormalMatrix(NormalMatrix, modelViewMatrix.GetMatrix());
		glUniformMatrix3fv(hUniNormalMatrix, 1, GL_FALSE, NormalMatrix);

		//Set Light Position
		M3DVector4f lightPosEye;
		m3dTransformVector4(lightPosEye, lightPos, mCamera);
		glUniform4fv(hUniLightPos, 1, lightPosEye);

		sphere.Draw();
	}
	modelViewMatrix.PopMatrix();
}

void GLView::DrawCube(M3DVector3f position){
	modelViewMatrix.PushMatrix();
	{
		M3DMatrix44f mCamera;
		camera.GetCameraMatrix(mCamera);

		modelViewMatrix.Translate(position[0], position[1], position[2]);

		M3DMatrix44f mMVP;
		m3dMatrixMultiply44(mMVP, projMatrix, modelViewMatrix.GetMatrix());
		glUniformMatrix4fv(hUniMVP, 1, GL_FALSE, mMVP);

		glUniformMatrix4fv(hUniModelView, 1, GL_FALSE, modelViewMatrix.GetMatrix());

		M3DMatrix33f NormalMatrix;
		GetNormalMatrix(NormalMatrix, modelViewMatrix.GetMatrix());
		glUniformMatrix3fv(hUniNormalMatrix, 1, GL_FALSE, NormalMatrix);

		M3DVector3f lightPosEye;
		m3dTransformVector3(lightPosEye, lightPos, modelViewMatrix.GetMatrix());

		glUniform3fv(hUniLightPos, 1, lightPosEye);

		cube.Draw();

	}
	modelViewMatrix.PopMatrix();
}
void GLView::GetInput(){
	short temp = 0;
	pressedmap = 0;

	if (GetAsyncKeyState('W'))
		temp |= (1 << W);
	if (GetAsyncKeyState('A'))
		temp |= (1 << A);
	if (GetAsyncKeyState('S'))
		temp |= (1 << S);
	if (GetAsyncKeyState('D'))
		temp |= (1 << D);
	if (GetAsyncKeyState(VK_NUMPAD4))
		temp |= (1 << K4);
	if (GetAsyncKeyState(VK_NUMPAD5))
		temp |= (1 << K5);
	if (GetAsyncKeyState(VK_NUMPAD6))
		temp |= (1 << K6);
	if (GetAsyncKeyState(VK_NUMPAD7))
		temp |= (1 << K7);
	if (GetAsyncKeyState(VK_NUMPAD8))
		temp |= (1 << K8);
	if (GetAsyncKeyState(VK_NUMPAD9))
		temp |= (1 << K9);
	if (GetAsyncKeyState(VK_RBUTTON))
		temp |= (1 << M2);

	for (int i = 0; i < NUMINPUT; i++){
		if ((keymap & (1 << i)) == 0 && (temp & (1 << i))){
			pressedmap |= (1 << i);
		}
	}

	keymap = temp;
}
void GLView::MoveCamera(){

	GetInput();

	float timePassed = stopwatch.GetElapsedSeconds();

	if (GetAsyncKeyState('W'))
		//if (pressedmap & (1 << W) == W)
		camera.MoveForward(moveSpeed * timePassed);

	if (GetAsyncKeyState('S'))
		camera.MoveForward(-moveSpeed * timePassed);

	if (GetAsyncKeyState('A'))
		camera.MoveRight(moveSpeed * timePassed);

	if (GetAsyncKeyState('D'))
		camera.MoveRight(-moveSpeed * timePassed);

	if (GetAsyncKeyState('Q'))
		camera.RotateLocal(m3dDegToRad(-turnSpeed) * timePassed, 0.0f, 0.0f, 1.0f);

	if (GetAsyncKeyState('E'))
		camera.RotateLocal(m3dDegToRad(turnSpeed) * timePassed, 0.0f, 0.0f, 1.0f);

	if (GetAsyncKeyState('R'))
		camera.RotateLocalX(m3dDegToRad(-turnSpeed)* timePassed);

	if (GetAsyncKeyState('C'))
		camera.RotateLocalX(m3dDegToRad(turnSpeed)* timePassed);

	if (GetAsyncKeyState(VK_SHIFT))
		camera.MoveUp(-moveSpeed * timePassed);

	if (GetAsyncKeyState(VK_SPACE))
		camera.MoveUp(moveSpeed * timePassed);

	if (GetAsyncKeyState(VK_RBUTTON))
		MouseMove();


	if (GetAsyncKeyState('T'))
		bNormalToggle = true;

	if (GetAsyncKeyState('F'))
		bNormalToggle = false;

	stopwatch.Reset();

}

void GLView::MouseMove(){
	static POINT initialPressPos = { 0, 0 };
	static POINT mousePos = { 0, 0 };

	if (pressedmap & (1 << M2))
		GetCursorPos(&initialPressPos);

	bool mouse = GetCursorPos(&mousePos);


	float rotY, rotX;
	rotY = initialPressPos.x - mousePos.x;
	rotX = mousePos.y - initialPressPos.y;

	camera.RotateLocalY(m3dDegToRad(rotY));
	camera.RotateLocalX(m3dDegToRad(rotX));

	initialPressPos = mousePos;




}
